package com.stn.zjx.givingtreasure.activity.userwallet;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.came.viewbguilib.ButtonBgUi;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.userbankcard.AddUserBankCardActivity;
import com.stn.zjx.givingtreasure.adapter.SelectBankCardItemListAdapter;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.bean.BankCard;
import com.stn.zjx.givingtreasure.domain.BankCardDomain;
import com.stn.zjx.givingtreasure.event.BankCardEvent;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.net.BaseApi;
import com.stn.zjx.givingtreasure.net.impl.UserBankCardNetImpl;
import com.stn.zjx.givingtreasure.service.UserService;
import com.stn.zjx.givingtreasure.table.User;
import com.vondear.rxtools.RxActivityTool;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserSelectBankCardActivity extends BaseActivity {
    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.web_title)
    TextView webTitle;
    @BindView(R.id.action_add_bt)
    RelativeLayout actionAddBt;
    @BindView(R.id.action_refresh)
    RelativeLayout actionRefresh;
    @BindView(R.id.exit)
    RelativeLayout exit;
    @BindView(R.id.sure_bt)
    TextView sureBt;
    @BindView(R.id.skip_bt)
    TextView skipBt;
    @BindView(R.id.share_bt)
    RelativeLayout shareBt;
    @BindView(R.id.more_bt)
    RelativeLayout moreBt;
    @BindView(R.id.scan_bt)
    RelativeLayout scanBt;
    @BindView(R.id.release_bt)
    ButtonBgUi releaseBt;
    @BindView(R.id.search_iv)
    ImageView searchIv;
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.search_bg)
    RelativeLayout searchBg;
    @BindView(R.id.save_bt)
    TextView saveBt;
    @BindView(R.id.rv)
    RecyclerView rv;
     public int userBankCardId=0;
    private LinearLayoutManager lm;
    private SelectBankCardItemListAdapter adapter;

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public void initComponent() {
        actionAddBt.setVisibility(View.VISIBLE);
        actionTitle.setText(getString(R.string.select_bank_card));
        userBankCardId=getIntent().getIntExtra("userBankCardId",0);
        lm=new LinearLayoutManager(this, OrientationHelper.VERTICAL,false);
        rv.setLayoutManager(lm);
        rv.setAdapter(adapter=new SelectBankCardItemListAdapter(this));
        initRefresh();
    }
   @Subscribe(threadMode = ThreadMode.MAIN)
   public void AddBankCardEvent(BankCardEvent event){
         switch (event.flag){
             case BankCardEvent.Add_Select_Bank_Card:
                  initRefresh();
                 break;
         }
   }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_bank_card_list);
        ButterKnife.bind(this);
        initComponent();
    }

    @OnClick({R.id.back_bt,R.id.action_add_bt})
    public void onViewClicked(View view) {
        Bundle bundle=new Bundle();
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;
            case R.id.action_add_bt:
                bundle.putInt("flag",BankCardEvent.Add_Select_Bank_Card);
                RxActivityTool.skipActivity(this, AddUserBankCardActivity.class,bundle);
                break;

        }
    }


    public void initRefresh(){
        User onlineUser= UserService.getInstance(this)
                .getOnlineLoginUser();
        if(onlineUser==null){
            return;
        }
        UserBankCardNetImpl.query_bank_card_by_user_id(onlineUser.getToken(), onlineUser.getToken() + "_query_bank_card_by_user_id", new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if(response.isSuccessful()&&!isFinishing()){
                    initBankCard(response.body());
                }
            }

            @Override
            public void onCacheSuccess(Response<String> response) {
                super.onCacheSuccess(response);
                if(response.isSuccessful()&&!isFinishing()){
                    initBankCard(response.body());
                }
            }
        });
    }
    @SuppressLint("SetTextI18n")
    public void initBankCard(String body) {
        BankCardDomain domain = new Gson().fromJson(body, BankCardDomain.class);
        switch (domain.getStatus()) {
            case BaseApi.REQUEST_SUCCESS_CODE:
                if (domain.getData() != null && domain.getData().size() != 0) {

                          adapter.refreshList(domain.getData());
                } else {
                    adapter.clearList();
                }
                break;
            default:
                adapter.clearList();
                break;
        }
    }
}
